package com.lfy.kcat.content.job;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfy.kcat.content.biz.BloomFilterTemplate;
import com.lfy.kcat.content.config.RedisConst;
import com.lfy.kcat.content.domain.Dramas;
import com.lfy.kcat.content.mapper.DramasMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author leifengyang
 * @version 1.0
 * @date 2025/9/22 10:54
 * @description:
 */
@Slf4j
@Component
public class BloomFilterJob {

    @Autowired
    private BloomFilterTemplate bloomFilterTemplate;

    @Autowired
    DramasMapper dramasMapper;

    @Autowired
    RedissonClient redissonClient;


    /**
     * 布隆重建
     */
    @XxlJob("rebuildBloomFilter")
    public void rebuildBloomFilter(){
        RBloomFilter<Object> filter = redissonClient.getBloomFilter(RedisConst.DRAMA_BF + ":temp");
        boolean b = filter.tryInit(10000000, 0.001);
        if (b){

            Long currentPage = 1L;

            //可能很慢
            while (true) {
                //自动翻页
                Page<Dramas> page = new Page<>(currentPage, 1000L);
                Page<Dramas> result = dramasMapper.selectPage(page, Wrappers.emptyWrapper());
                for (Dramas record : result.getRecords()) {
                    //每个短剧往进加
                    log.info("重建布隆，短剧id：{}",record.getDramaId());
                    filter.add(record.getDramaId());
                }

                if (result.hasNext()) {
                    //有下一页就翻页
                    currentPage++;
                }else {
                    break;
                }
            }

            //新bloom完成
            RBloomFilter<Object> oldBfilter = redissonClient.getBloomFilter(RedisConst.DRAMA_BF);
            oldBfilter.rename("hahahahah");
            //给新布隆命名
            filter.rename(RedisConst.DRAMA_BF);
            //删除老布隆
            oldBfilter.delete();

        }



    }

    /**
     * 每天晚上3点添加数据： 只增不删
     * 从数据库中查询所有的短剧id，并添加到布隆过滤器中
     */
    @XxlJob("JobAllDramaIdAndAddToBloomFilter")
    public void getAllDramaIdAndAddToBloomFilter() {
        //1、从数据库中查询所有的短剧id
        //如果数据量太大，应该分页查询
        //分页把所有精选短剧都放到redis

        Long currentPage = 1L;
        while (true) {
            //自动翻页
            Page<Dramas> page = new Page<>(currentPage, 1000L);
            Page<Dramas> result = dramasMapper.selectPage(page, Wrappers.emptyWrapper());
            for (Dramas record : result.getRecords()) {
                //每个短剧往进加
                log.info("添加到布隆过滤器的短剧id：{}",record.getDramaId());
                bloomFilterTemplate.addDramaIdBloomFilter(record.getDramaId());
            }

            if (result.hasNext()) {
                //有下一页就翻页
                currentPage++;
            }else {
                break;
            }
        }
    }
}
